package com.dqx.haoqiu.mvp.common.base;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dqx.haoqiu.mvp.R;
import com.dqx.haoqiu.mvp.utils.Navigation;
import com.xmjs.baselibrary.base.IBasePresenter;
import com.xmjs.baselibrary.base.dialog.RxDialog;
import com.xmjs.baselibrary.utils.LocalCache;
import com.xmjs.baselibrary.utils.SP;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

/**
 * Dialog基类：
 * 1.从下至上弹出
 * 2.提供错误提示方法
 *
 * @param <P>
 */
public class BaseDialog<P extends IBasePresenter> extends RxDialog {
    private View v_base_body;
    private RelativeLayout rl_base_body;
    private RelativeLayout rl_base_error;
    private TextView tv_base_error;
    private Button btn_base_error;
    private RelativeLayout rl_base_loading;
    private ImageView iv_base_loading;

    private Activity activity;

    @Inject
    public P presenter;

    public BaseDialog(@NonNull Activity activity) {
        super(activity, R.style.BaseDialogStyle);
        this.activity = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.dialog_base);


        setCancelable(true);
        setCanceledOnTouchOutside(true);

        //设置宽高与对齐方式
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = setWidth();
        lp.height = setHeight();
        dialogWindow.setGravity(setGravity());

        //父容器
        rl_base_body = findViewById(R.id.rl_base_body);

        //loading视图遮罩
        rl_base_loading = findViewById(R.id.rl_base_loading);
        iv_base_loading = findViewById(R.id.iv_base_loading);
        Glide.with(getContext()).load(R.drawable.loading).asGif().into(iv_base_loading);

        //错误提示的遮罩
        rl_base_error = findViewById(R.id.rl_base_error);
        rl_base_error.setVisibility(GONE);
        tv_base_error = findViewById(R.id.tv_base_error);
        btn_base_error = findViewById(R.id.btn_base_error);
        btn_base_error.setOnClickListener(v -> this.onReload());
    }

    /**
     * 设置布局
     *
     * @param resId
     */
    @Override
    public void setContentView(int resId) {
        v_base_body = getLayoutInflater().inflate(resId, null);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout
                .LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        if (null != rl_base_body) rl_base_body.addView(v_base_body, params);
    }

    public Activity getActivity() {
        return activity;
    }

    /**
     * 设置宽度
     */
    public int setWidth() {
        return WindowManager.LayoutParams.MATCH_PARENT;
    }

    /**
     * 设置高度
     */
    public int setHeight() {
        return WindowManager.LayoutParams.WRAP_CONTENT;
    }

    /**
     * 设置对齐方式
     */
    public int setGravity() {
        return Gravity.BOTTOM;
    }

    /**
     * 显示加载对话框
     *
     * @param cancelable 是否可以取消
     */
    public void showLoadingView(boolean cancelable) {
        rl_base_loading.setVisibility(VISIBLE);
        rl_base_loading.setOnClickListener(view -> {
            if (cancelable)
                rl_base_loading.setVisibility(GONE);
        });
    }

    /**
     * 隐藏加载对话框
     */
    public void hideLoadingView() {
        rl_base_loading.setVisibility(GONE);
    }

    public void toast(String msg) {

    }

    public void toast(int msgId) {

    }

    public void onReLogin() {
        //清空用户缓存数据
        LocalCache.clear(new SP(getContext()));
        //打开登录页面
        Navigation.openLogin((Activity) getContext());
    }

    /**
     * 当接口请求出现错误，需要重新加载页面或重新请求接口时，请重写该的方法
     */
    public void onReload() {
        rl_base_error.setVisibility(GONE);
    }

    /**
     * 显示页面错误提示
     *
     * @param iconResId    图标资源id
     * @param hintResId    提示文案id
     * @param btnTextResId 按钮文案id
     */
    public void showError(int iconResId, int hintResId, int btnTextResId) {
        rl_base_error.setVisibility(VISIBLE);
        tv_base_error.setVisibility(VISIBLE);
        btn_base_error.setVisibility(VISIBLE);
        tv_base_error.setCompoundDrawablesWithIntrinsicBounds(0, iconResId, 0, 0);
        tv_base_error.setText(hintResId);
        btn_base_error.setText(btnTextResId);
        rl_base_error.setBackgroundResource(R.color.color_theme);
    }

    /**
     * 显示页面错误提示
     *
     * @param iconResId    图标资源id
     * @param errorText    错误文案
     * @param btnTextResId 按钮文案id
     */
    public void showError(int iconResId, String errorText, int btnTextResId) {
        rl_base_error.setVisibility(VISIBLE);
        tv_base_error.setVisibility(VISIBLE);
        btn_base_error.setVisibility(VISIBLE);
        tv_base_error.setCompoundDrawablesWithIntrinsicBounds(0, iconResId, 0, 0);
        tv_base_error.setText(errorText);
        btn_base_error.setText(btnTextResId);
        rl_base_error.setBackgroundResource(R.color.color_theme);
    }

    /**
     * 显示空提示
     *
     * @param isShow    是否显示
     * @param iconResId 图标资源id
     * @param hintResId 提示文案id
     */
    public void showEmpty(boolean isShow, int iconResId, int hintResId) {
        rl_base_error.setVisibility(isShow ? VISIBLE : GONE);
        tv_base_error.setVisibility(isShow ? VISIBLE : GONE);
        btn_base_error.setVisibility(GONE);
        tv_base_error.setCompoundDrawablesWithIntrinsicBounds(0, iconResId, 0, 0);
        tv_base_error.setText(hintResId);
        rl_base_error.setBackgroundResource(R.color.transparent);
    }

    /**
     * 显示空提示
     *
     * @param isShow          是否显示
     * @param iconResId       图标资源id
     * @param hintResId       提示文案id
     * @param backgroundResId 背景资源id
     */
    public void showEmpty(boolean isShow, int iconResId, int hintResId, int backgroundResId) {
        rl_base_error.setVisibility(isShow ? VISIBLE : GONE);
        tv_base_error.setVisibility(isShow ? VISIBLE : GONE);
        btn_base_error.setVisibility(GONE);
        tv_base_error.setCompoundDrawablesWithIntrinsicBounds(0, iconResId, 0, 0);
        tv_base_error.setText(hintResId);
        rl_base_error.setBackgroundResource(backgroundResId);
    }

    /**
     * 显示默认的空提示
     *
     * @param isShow 是否显示
     */
    public void showEmpty(boolean isShow) {
        showEmpty(isShow, R.mipmap.ic_me_follow_sebg, R.string.no_data);
    }
}
